forEach 和 for迴圈


Posted by hoyi-23 on 2021-05-10

forEach和for迴圈

forEach 基本上可以達成for迴圈的所有需求
很大的差異在於 : for 可以被中斷 break; forEach不可被中斷!!!

//此為for loop
var array=['1','2','3'];
for(var i=0;i<array.length;i++){
console.log(i,array[i])
}
//此為forEach
array.forEach(function(array[i],i){
console.log(i,array[i])
}

forEach傳入的參數有三,大多只使用前兩者
1.單一物件 (單一陣列的值)
2.物件索引 (陣列的索引值)
3.完整的陣列本體 (陣列本體array)










Related Posts

The introduction and difference between class component and function component in React

The introduction and difference between class component and function component in React

Print lots of stars patterns

Print lots of stars patterns

找出陣列中的最大值

找出陣列中的最大值


Comments